// pages/cardList/cardList.js
const app = getApp()
var request = require("../../utils/request.js");
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    windowHeight: 0,
    scrollViewHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // 先取出页面高度 windowHeight
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
    let scrollViewHeight = that.data.windowHeight - that.data.statusBarHeight - 44 - 27;
    that.setData({
      scrollViewHeight: scrollViewHeight
    })
    that.getBuyList();
  },
  /**
   * 选择时间
   * @param {*} event 
   */
  startStartTime(event) {
    let that = this;
    let rechargeId = event.currentTarget.dataset.id;
    let userId = wx.getStorageSync('userId');
    let beginTime = event.detail.value;
    that.setData({
      checkStartDate: beginTime
    })
    let url = "/api/cUser/updateBeginTimeByRechargeId?userId=" + userId + "&rechargeId=" + rechargeId + "&beginTime=" + beginTime;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          Toast.success('修改成功');
          that.getBuyList();
        } else {
          Toast.fail('修改失败');
        }
      }).catch((errMsg) => {
        // wx.showToast({
        //   title: '修改失败',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 查看更多
   * @param {*} event 
   */
  seeMore(event) {
    let parks = event.currentTarget.dataset.parks;
    wx.showModal({
      title: '使用车场',
      content: parks,
      showCancel: false
    })
  },
  /**
   * 获取购买电子卡记录
   */
  getBuyList() {
    let that = this;
    let userId = wx.getStorageSync('userId');
    let url = "/api/bParkingleaserecord/getUsingAndNoUseCard?userId=" + userId;
    request.http(url, 'POST')
      .then((res) => {
        if (res.data.code == 200) {
          let list = res.data.data;
          if (list != null && list.length > 0) {
            for (let i = 0; i < list.length; i++) {
              if (i == 0) {
                list[i].seleted = true;
              } else {
                list[i].seleted = false;
              }
            }
            that.setData({
              list: list
            })
          }
        }
      }).catch((errMsg) => {
        // wx.showToast({
        //   title: '暂无数据',
        //   icon: 'loading',
        //   duration: 1000,
        // })
      });
  },
  /**
   * 是否展示更多
   * @param {*} event 
   */
  changeSelect(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let list = that.data.list;
    list[index].seleted = !list[index].seleted;
    that.setData({
      list: list
    })
  },
})